define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recovery/category/index' + location.search,
                    add_url: 'recovery/category/add',
                    edit_url: 'recovery/category/edit',
                    del_url: 'recovery/category/del',
                    // multi_url: 'recovery/category/multi',
                    // import_url: 'recovery/category/import',
                    table: 'recovery_category',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // searchFormVisible: true,
                pagination: false,
                commonSearch: false,
                search: false,
                rowAttributes: function (row, index) {
                    return row.pid == 0 ? {} : {style: "display:none"};
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false, visible: false},
                        {field: 'pid', title: __('Pid'), operate: false, visible: false},
                        {field: 'code', title: __('Code'), operate: false},
                        {field: 'name', title: __('Name'), align: 'left', operate: 'LIKE',
                            formatter:function(value,row,index){
                                if(row.pid!="0"){
                                    value = "&nbsp;&nbsp;&nbsp;&nbsp;"+value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                                }
                                return '<a href="javascript:;" style="color: #333333; background-color: unset; border: unset;" data-toggle="tooltip" title="' + __('点击显示子分类') + '" data-id="' + row.id + '" data-pid="' + row.pid + '"  class="btn btn-xs '
                                    + (row.pid == 0 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub">'+value+'</a>';
                            }
                        },
                        {field: 'unit', title: __('Unit'), operate: false},
                        {field: 'price', title: __('Price'), operate: false,
                            formatter:function(value,row,index){
                                if(value=="0.00"){
                                    value="";
                                }
                                return value;
                            },
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //显示隐藏子节点
            $(document).on("click", ".btn-node-sub", function (e) {
                var status = $(this).data("shown") ? true : false;
                $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
                    $(this).closest("tr").toggle(!status);
                });
                $(this).data("shown", !status);
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});